/**
*
* @file
*		heap_ludmila
*
* @brief
*		Heap implementation by ludmila
*/

#ifndef __HEAP_LUDMILA_H__
#define __HEAP_LUDMILA_H__

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include "../../api/heap.h"
#include <iostream>
#include <vector>
#include <map>
using namespace std;

#define POISON_ALLOC 0x0A0A0A0A
#define POISON_FREE 0x0F0F0F0F

/**
* basic interface
*/

class heap_ludmila: public heap876
{

private:
	// current volume of distributed memory
	size_t alloc_sz;
	// pointer to heap
	void* heap_address;
	// minimal allocation space
	static const size_t block_size = 128;

	char* end_of_heap;

	struct mem_block
	{
		size_t size;
		unsigned int tag;
	};

	map<void*, mem_block> mem_map;

	HANDLE mutex;

	void* find_block(size_t size);

	int check_poison(void* ptr, size_t size);

	void free_base(void *ptr, map<void*, mem_block>::iterator iter);

public:
	heap_ludmila( size_t total, alloc_flags flag );
	virtual ~heap_ludmila();
	virtual void*	alloc( size_t sz, unsigned int tag );
	virtual void	free( void* ptr );

	virtual void	free_all( unsigned int tag );

	virtual size_t get_occupancy() const { return alloc_sz; };

	void print_bit_map();

	void print_allocation_space();

	void statistics();

};

#endif//__HEAP_LUDMILA_H__